import {defineStore} from 'pinia'
import {isEnvProd} from '@/config'
import {encrypt, decrypt} from '@/utils/encrypt'
import {TABBAR_LIST} from '@/constant/enums';

export interface IAppState {
    token: string
    openInstallInstance: OpenInstall | null,
    [key: string]: any
}

export const useAppStore = defineStore({
    id: 'app',
    state: () => {
        return {
            token: '',
            openInstallInstance: null,
            TABBAR_LIST,
        } as IAppState

    },
    getters: {
        getTabBar: (state) => state.TABBAR_LIST,
    },
    actions: {
        setToken(token: string) {
            this.token = `Bearer ${token}`
        },
        setOpenInstall(instance: OpenInstall) {
            this.openInstallInstance = instance
        }
    },
    // 开启数据缓存
    persist: {
        key: 'app',
        storage: window.sessionStorage,
        serializer: {
            serialize: isEnvProd ? encrypt : JSON.stringify,
            deserialize: isEnvProd ? decrypt : JSON.parse
        }
    }
})
